<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${daoUrl}.${entityName}Dao">
    <resultMap id="BaseResultMap" type="${entityUrl}.${entityName}">
        <#list cis as ci>
            <id column="${ci.column}" jdbcType="${ci.jdbcType?upper_case}" property="${ci.property}"/>
        </#list>
    </resultMap>
    <sql id="Base_Column_List">
        ${agile}
    </sql>


    <!-- 查询 -->
    <#if idType??>
        <#if (keys?size==1)>

            <select id="selectByPrimaryKey" parameterType="java.lang.${idType}"
                    resultMap="BaseResultMap">
                select
                <include refid="Base_Column_List"/>
                from ${table}
                where ${keyField}= ${r'#{'}${keyField},jdbcType=${idJdbcType?upper_case}}
            </select>

            <!-- 删除 -->
        <#--	<delete id="deleteByPrimaryKey" parameterType="java.lang.${idType}">-->

            <delete id="deleteByPrimaryKey" parameterType="java.lang.${idType}">
                delete from ${table}
                where ${keyField} = ${r'#{'}${keyField},jdbcType=${idJdbcType?upper_case}}
            </delete>
        <#else>
            <select id="selectByPrimaryKeys" parameterType="java.lang.${idType}"
                    resultMap="BaseResultMap">
                select
                <include refid="Base_Column_List"/>
                from ${table}
                <where>
                    <#list keys as ci>
                        <if test="${ci.property}  != null">
                            AND ${ci.column} = ${r'#{'}${ci.property} ,jdbcType=${ci.jdbcType?upper_case}}
                        </if>
                    </#list>
                </where>
            </select>


            <delete id="deleteByPrimaryKeys" parameterType="${entityUrl}.${entityName}">
                delete from ${table}
                <where>
                    <#list keys as ci>
                        <if test="${ci.property}  != null">
                            AND ${ci.column} = ${r'#{'}${ci.property} ,jdbcType=${ci.jdbcType?upper_case}}
                        </if>
                    </#list>
                </where>
            </delete>
        </#if>

    </#if>

    <delete id="deleteBySelective" parameterType="${entityUrl}.${entityName}">
        delete from ${table}
        <where>
            <#list cis as ci>

                <if test="${ci.property}  != null">
                    AND ${ci.column} = ${r'#{'}${ci.property} ,jdbcType=${ci.jdbcType?upper_case}}
                </if>
            </#list>
        </where>
    </delete>
    <!-- 选择添加 -->
    <insert id="insertSelective" parameterType="${entityUrl}.${entityName}">
        insert into ${table}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list cis as ci>
                <if test="${ci.property} != null">
                    ${ci.column},
                </if>
            </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#list cis as ci>
                <if test="${ci.property} != null">
                    ${r'#{'}${ci.property},jdbcType=${ci.jdbcType?upper_case}},
                </if>
            </#list>
        </trim>
    </insert>
    <!-- 选择修改 -->
    <#if (keys?size==1)>
        <update id="updateByPrimaryKeySelective" parameterType="${entityUrl}.${entityName}">
            update ${table}
            <set>
                <#list cis as ci>
                    <if test="${ci.property} != null">
                        ${ci.column} = ${r'#{'}${ci.property},jdbcType=${ci.jdbcType?upper_case}},
                    </if>
                </#list>
            </set>
            where ${keyField} = ${r'#{'}${keyField},jdbcType=${idJdbcType}}
        </update>
    <#else>
        <update id="updateByPrimaryKeySelectives" parameterType="${entityUrl}.${entityName}">
            update ${table}
            <set>
                <#list cis as ci>
                    <if test="${ci.property} != null">
                        ${ci.column} = ${r'#{'}${ci.property},jdbcType=${ci.jdbcType?upper_case}},
                    </if>
                </#list>
            </set>
            <where>
                <#list cis as ci>
                    <if test="${ci.property}  != null">
                        AND ${ci.column} = ${r'#{'}${ci.property} ,jdbcType=${ci.jdbcType?upper_case}}
                    </if>
                </#list>
            </where>
        </update>
    </#if>
    <select id="query${entityName}All" parameterType="${entityUrl}.${entityName}"
            resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ${table}
    </select>
    <!-- 组合条件查询 -->
    <select id="query${entityName}List" parameterType="${entityUrl}.${entityName}"
            resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ${table}
        <where>

            <#list cis as ci>

                <if test="${ci.property}  != null">
                    AND ${ci.column} = ${r'#{'}${ci.property} ,jdbcType=${ci.jdbcType?upper_case}}
                </if>
            </#list>
        </where>
    </select>


    <select id="selectByExample" parameterType="springmvc.common.CriteriaExample"
            resultType="${entityUrl}.${entityName}">
        select * from ${table}
        <if test="_parameter != null">
            <where>
                <include refid="Example_Where_Clause"/>
            </where>
        </if>
        <include refid="orderBy_case"/>
    </select>

    <update id="updateByExample" parameterType="java.util.Map">
        update ${table}
        <set>
            <#list cis as ci>
                <if test="${ci.property} != null">
                    ${ci.column} = ${r'#{'}${objectName}.${ci.property},jdbcType=${ci.jdbcType?upper_case}},
                </if>
            </#list>
        </set>
        <if test="_parameter.get(0) != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
    </update>

    <delete id="deleteList" parameterType="s${entityUrl}.${entityName}">
        delete from ${table}
        <where>
            <#list cis as ci>

                <if test="${ci.property}  != null">
                    AND ${ci.column} = ${r'#{'}${ci.property} ,jdbcType=${ci.jdbcType?upper_case}}
                </if>
            </#list>
        </where>
    </delete>


    <delete id="deleteByExample" parameterType="springmvc.common.CriteriaExample">
        delete from ${table}
        <if test="_parameter != null">
            <where>
                <include refid="Example_Where_Clause"/>
            </where>
        </if>
    </delete>


    <select id="getCountByExample" parameterType="springmvc.common.CriteriaExample">
        select count(*) from ${table}
        <if test="_parameter != null">
            <where>
                <include refid="Example_Where_Clause"/>
            </where>
        </if>
    </select>

    <sql id="Example_Where_Clause">
        <if test="example != null">
            <if test="example.criteriaList!= null">
                <foreach collection="example.criteriaList" item="criteria"
                         separator="AND">
                    <trim prefix="(" suffix=")">
                        <foreach collection="criteria.criteria" item="criterion"
                                 separator="OR">
                            <choose>
                                <when test="criterion.noValue">
                                    ${r'${'}criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    ${r'${'} criterion.condition}} ${r'#{'}criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    ${r'${'} criterion.condition ${r'#{'}criterion.value} AND
                                    ${r'#{'} criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    ${r'${'}criterion.condition}
                                    <foreach collection="criterion.value" item="listItem"
                                             open="(" close=")" separator=",">
                                        ${r'#{'} listItem}}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </foreach>
            </if>
        </if>
    </sql>


    <sql id="orderBy_case">
        <if
                test="example.orderByList !=null and example.orderByList.size() >0">
            ORDER BY
            <foreach collection="example.orderByList" item="orderBy"
                     separator=",">
                ${r'${'}orderBy.condition} ${r'${'}orderBy.direction}
            </foreach>
        </if>
    </sql>


    <sql id="Update_By_Example_Where_Clause">
        <if test="example != null">
            <if test="example.criteriaList!= null">
                <where>
                    <!-- 传入集合类型是example.oredCriteria 循环变量是criteria -->
                    <foreach collection="example.criteriaList" item="criteria"
                             separator="or">
                        <if test="criteria.valid">
                            <trim prefix="(" suffix=")" prefixOverrides="and">
                                <foreach collection="criteria.criteria" item="criterion">
                                    <!-- 结构是条件判断如果满足就追加 -->
                                    <choose>
                                        <when test="criterion.noValue">
                                            and ${r'${'} criterion.condition}
                                        </when>
                                        <when test="criterion.singleValue">
                                            and ${r'${'}criterion.condition} ${r'#{'}criterion.value}
                                        </when>
                                        <when test="criterion.betweenValue">
                                            and ${r'${'}criterion.condition} ${r'#{'}criterion.value}
                                            and
                                            ${r'#{'} criterion.secondValue}
                                        </when>
                                        <when test="criterion.listValue">
                                            and ${r'${'}criterion.condition}
                                            <foreach collection="criterion.value" item="listItem"
                                                     open="(" close=")" separator=",">
                                                ${r'#{'}listItem}
                                            </foreach>
                                        </when>
                                    </choose>
                                </foreach>
                            </trim>
                        </if>
                    </foreach>
                </where>
            </if>
        </if>
    </sql>

</mapper>